package com.qianfeng.xinyihezi.xinyihezi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.ecloud.pulltozoomview.PullToZoomScrollViewEx;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.activity.activity_meng.AddressActivity;
import com.qianfeng.xinyihezi.xinyihezi.activity.activity_meng.LoginActivity;
import com.qianfeng.xinyihezi.xinyihezi.model.bean_meng.MySelf;
import com.qianfeng.xinyihezi.xinyihezi.utils.utils_meng.GlideCircleTransform;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2016/11/12.
 */

public class MeFragment extends Fragment implements View.OnClickListener {

    private PullToZoomScrollViewEx scrollView;
    private TextView mTextViewName;
    private ImageView mImageViewIcon;
    private ImageView mImageViewLogin;
    private TextView mTextViewSend;
    private TextView mTextViewReceive;
    private TextView mTextViewMoney;
    private ImageView mImageViewEdit;
    private TextView mTextViewBalance;
    private TextView mTextViewOr;
    private TextView mTextViewPacket;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, null);
        EventBus.getDefault().register(this);
        initView(view);
        return view;
    }

    private void initView(View view) {
        scrollView = (PullToZoomScrollViewEx) view.findViewById(R.id.scroll_view);
        loadViewForPullToZoomScrollView(scrollView);
        setPullToZoomViewLayoutParams(scrollView);
    }

    private void loadViewForPullToZoomScrollView(PullToZoomScrollViewEx scrollView) {

        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.profile_head_view, null);
        mTextViewBalance = (TextView) headView.findViewById(R.id.tv_profile_balance);
        mTextViewOr = (TextView) headView.findViewById(R.id.tv_profile_or);
        mTextViewPacket = (TextView) headView.findViewById(R.id.tv_profile_packet);
        mTextViewBalance.setOnClickListener(this);
        mTextViewOr.setOnClickListener(this);
        mTextViewPacket.setOnClickListener(this);

        mTextViewName = (TextView) headView.findViewById(R.id.tv_profile_head);
        mImageViewIcon = (ImageView) headView.findViewById(R.id.iv_profile_icon);
        mImageViewLogin = (ImageView) headView.findViewById(R.id.iv_profile_login);
        mImageViewEdit = (ImageView) headView.findViewById(R.id.iv_profile_edit);
        mImageViewEdit.setOnClickListener(this);
        RelativeLayout mRelativeLayout = (RelativeLayout) headView.findViewById(R.id.rl_login);
        mRelativeLayout.setOnClickListener(this);
        View zoomView = LayoutInflater.from(getActivity()).inflate(R.layout.list_head_zoom_view, null);
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.list_content_view, null);
        mTextViewSend = (TextView) contentView.findViewById(R.id.tv_send_gift);
        mTextViewReceive = (TextView) contentView.findViewById(R.id.tv_receive_gift);
        mTextViewMoney = (TextView) contentView.findViewById(R.id.tv_cou_money);
        LinearLayout mLinearLayout = (LinearLayout) contentView.findViewById(R.id.ll_content_view);
        LinearLayout mLinearLayoutLogin = (LinearLayout) contentView.findViewById(R.id.ll_content_login);
        RelativeLayout mRelativeLayoutService = (RelativeLayout) contentView.findViewById(R.id.rl_content_service);
        RelativeLayout mRelativeLayoutSetting = (RelativeLayout) contentView.findViewById(R.id.rl_content_setting);
        RelativeLayout mRelativeLayoutAddress = (RelativeLayout) contentView.findViewById(R.id.rl_content_address);
        RelativeLayout mRelativeLayoutQingGift = (RelativeLayout) contentView.findViewById(R.id.rl_content_qinggift);
        RelativeLayout mRelativeLayoutInvite = (RelativeLayout) contentView.findViewById(R.id.rl_content_invite);
        RelativeLayout mRelativeLayoutCollection = (RelativeLayout) contentView.findViewById(R.id.rl_content_collection);
        mLinearLayout.setOnClickListener(this);
        mLinearLayoutLogin.setOnClickListener(this);
        mRelativeLayoutService.setOnClickListener(this);
        mRelativeLayoutSetting.setOnClickListener(this);
        mRelativeLayoutAddress.setOnClickListener(this);
        mRelativeLayoutQingGift.setOnClickListener(this);
        mRelativeLayoutInvite.setOnClickListener(this);
        mRelativeLayoutCollection.setOnClickListener(this);
        mTextViewSend.setOnClickListener(this);
        mTextViewReceive.setOnClickListener(this);
        mTextViewMoney.setOnClickListener(this);
        scrollView.setHeaderView(headView);
        scrollView.setZoomView(zoomView);
        scrollView.setScrollContentView(contentView);
    }


    // 设置头部的View的宽高。
    private void setPullToZoomViewLayoutParams(PullToZoomScrollViewEx scrollView) {
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        int mScreenHeight = localDisplayMetrics.heightPixels;
        int mScreenWidth = localDisplayMetrics.widthPixels;
        LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth,
                (int) (12.0F * (mScreenWidth / 16.0F)));
        scrollView.setHeaderLayoutParams(localObject);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_profile_balance:
                Toast.makeText(getActivity(), mTextViewBalance.getText().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_profile_packet:
                Toast.makeText(getActivity(), mTextViewPacket.getText().toString(), Toast.LENGTH_SHORT).show();
                break;

            case R.id.rl_login:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                }
                break;
            case R.id.iv_profile_edit:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "编辑", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_send_gift:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "送出的礼物", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_receive_gift:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "收到的礼物", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_cou_money:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "凑份子", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.rl_content_address:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    startActivity(new Intent(getActivity(), AddressActivity.class));
                }
                break;
            case R.id.rl_content_qinggift:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "轻礼物", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rl_content_invite:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "邀请好友", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rl_content_collection:
                if (mTextViewName.getText().toString().equals("欢迎来到心意盒子 您尚未登录")) {
                    onTurnToLogin();
                } else {
                    Toast.makeText(getActivity(), "我的收藏", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rl_content_service:
                Toast.makeText(getActivity(), "客服中心", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rl_content_setting:
                Toast.makeText(getActivity(), "设置", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

    public void onTurnToLogin() {
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }

    @Subscribe(priority = 99, threadMode = ThreadMode.MAIN)
    public void GetMyInfo(MySelf mySelf) {
        Log.e("123", mySelf.name + "From:" + mySelf.icon);
        mTextViewName.setText(mySelf.name);
        mImageViewLogin.setVisibility(View.GONE);
        mTextViewBalance.setVisibility(View.VISIBLE);
        mTextViewOr.setVisibility(View.VISIBLE);
        mTextViewPacket.setVisibility(View.VISIBLE);
        Glide.with(this).load(mySelf.icon)
                .transform(new GlideCircleTransform(getActivity()))
                .into(mImageViewIcon);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
